#!/bin/bash

# 备份原始配置文件
if [ -f /etc/yum.conf ]; then
    cp /etc/yum.conf /etc/yum.conf.bak
    echo "已备份 /etc/yum.conf"
fi

if [ -f /etc/dnf/dnf.conf ]; then
    cp /etc/dnf/dnf.conf /etc/dnf/dnf.conf.bak
    echo "已备份 /etc/dnf/dnf.conf"
fi

# 修复YUM配置
if [ -f /etc/yum.conf ]; then
    if grep -q "^gpgcheck=" /etc/yum.conf; then
        # 如果存在gpgcheck行，修改为1
        sed -i 's/^gpgcheck=.*/gpgcheck=1/' /etc/yum.conf
    else
        # 如果不存在，在[main]段后添加
        sed -i '/^\[main\]/a gpgcheck=1' /etc/yum.conf
    fi
    echo "YUM gpgcheck已启用"
fi

# 修复DNF配置
if [ -f /etc/dnf/dnf.conf ]; then
    if grep -q "^gpgcheck=" /etc/dnf/dnf.conf; then
        # 如果存在gpgcheck行，修改为1
        sed -i 's/^gpgcheck=.*/gpgcheck=1/' /etc/dnf/dnf.conf
    else
        # 如果不存在，在[main]段后添加
        sed -i '/^\[main\]/a gpgcheck=1' /etc/dnf/dnf.conf
    fi
    echo "DNF gpgcheck已启用"
fi

echo "gpgcheck全局激活配置完成"

